package com.evan.kdl.test.day02.service;

import cn.hutool.core.util.ObjectUtil;
import com.evan.kdl.test.entity.HttpRequest;
import com.evan.kdl.test.entity.PriceResultVo;
import com.evan.kdl.util.CommonUtils;

import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author huangzhongliang
 * @time 2024/01/18 17:26
 */
public class ComparePriceService {
    // 方案一：串行方式操作商品比价
    public PriceResultVo getCheapestPlatformPrice(String productName) {
        PriceResultVo priceResultVo;
        int discount;
        // 获取淘宝平台的价格和优惠
        priceResultVo = HttpRequest.getTaoBaoPrice(productName);
        discount = HttpRequest.getTaoBaoDiscount(productName);
        PriceResultVo taoBaoPriceResultVo = this.computeRealPrice(priceResultVo, discount);

        // 获取京东平台的价格和优惠
        priceResultVo = HttpRequest.getJDongPrice(productName);
        discount = HttpRequest.getJDongDiscount(productName);
        PriceResultVo jDongPriceResultVo = this.computeRealPrice(priceResultVo, discount);


        // 获取拼多多平台的价格和优惠
        priceResultVo = HttpRequest.getPDDPrice(productName);
        discount = HttpRequest.getPDDDiscount(productName);
        PriceResultVo pddPriceResultVo = this.computeRealPrice(priceResultVo, discount);

        return Stream.of(taoBaoPriceResultVo, jDongPriceResultVo, pddPriceResultVo).min(Comparator.comparing(PriceResultVo::getRealPrice)).get();

    }


    // 计算商品的最终价格 = 平台价格 - 优惠价
    public PriceResultVo computeRealPrice(PriceResultVo priceResultVo, int discount) {
        priceResultVo.setRealPrice(priceResultVo.getPrice() - discount);
        priceResultVo.setDiscount(discount);
        CommonUtils.printThreadLog(priceResultVo.getPlatform() + "最终价格计算完成:" + priceResultVo.getRealPrice());
        return priceResultVo;
    }

    // 方案二：使用Future+线程池增强并行
    public PriceResultVo getCheapestPlatformPrice2(String productName) {
        ExecutorService executor = Executors.newFixedThreadPool(5);

        Future<PriceResultVo> taoBaoFuture = executor.submit(() -> {
            PriceResultVo priceResultVo = HttpRequest.getTaoBaoPrice(productName);
            int discount = HttpRequest.getTaoBaoDiscount(productName);
            return this.computeRealPrice(priceResultVo, discount);
        });

        // 获取京东平台的价格和优惠
        Future<PriceResultVo> jDongFuture = executor.submit(() -> {
            PriceResultVo priceResultVo = HttpRequest.getJDongPrice(productName);
            int discount = HttpRequest.getJDongDiscount(productName);
            return this.computeRealPrice(priceResultVo, discount);
        });


        // 获取拼多多平台的价格和优惠
        Future<PriceResultVo> pddFuture = executor.submit(() -> {
            PriceResultVo priceResultVo = HttpRequest.getPDDPrice(productName);
            int discount = HttpRequest.getPDDDiscount(productName);
            return this.computeRealPrice(priceResultVo, discount);
        });

        executor.shutdown();
        return Stream.of(taoBaoFuture, jDongFuture, pddFuture)
                .map(future -> {
                    try {
                        //加超时时间
                        return future.get();
                    } catch (Exception e) {
                        e.printStackTrace();
                        return null;
                    }
                })
                .filter(ObjectUtil::isNotEmpty)
                .min(Comparator.comparing(PriceResultVo::getRealPrice))
                .get();

    }

    // 方案三：使用 CompletableFuture 进一步增强并行
    public PriceResultVo getCheapestPlatformPrice3(String productName) {
        // 获取淘宝平台的价格和优惠
        CompletableFuture<PriceResultVo> taoBaoCF = CompletableFuture.supplyAsync(() -> HttpRequest.getTaoBaoPrice(productName))
                .thenCombine(CompletableFuture.supplyAsync(() -> {
                    return HttpRequest.getTaoBaoDiscount(productName);
                }), (priceResultVo, discount) -> {
                    return this.computeRealPrice(priceResultVo, discount);
                });

        // 获取京东平台的价格和优惠
        CompletableFuture<PriceResultVo> jDongCF = CompletableFuture
                .supplyAsync(() -> HttpRequest.getJDongPrice(productName))
                .thenCombine(CompletableFuture.supplyAsync(() -> HttpRequest.getJDongDiscount(productName)), this::computeRealPrice);
        // 获取拼多多平台的价格和优惠
        CompletableFuture<PriceResultVo> pddCF = CompletableFuture
                .supplyAsync(() -> HttpRequest.getPDDPrice(productName))
                .thenCombine(CompletableFuture.supplyAsync(() -> HttpRequest.getPDDDiscount(productName)), this::computeRealPrice);

        return Stream.of(taoBaoCF, jDongCF, pddCF)
                .map(CompletableFuture::join)
                .min(Comparator.comparing(PriceResultVo::getRealPrice))
                .get();
    }

    public PriceResultVo batchComparePrice(List<String> products) {
        // 遍历每个商品，根据商品开启异步任务获取最终价，然后归集到List集合
        List<CompletableFuture<PriceResultVo>> completableFutures = products.stream()
                .map(product -> CompletableFuture.supplyAsync(() -> HttpRequest.getTaoBaoPrice(product)).thenCombine(CompletableFuture.supplyAsync(() -> HttpRequest.getTaoBaoDiscount(product)), this::computeRealPrice))
                .collect(Collectors.toList());

        return completableFutures.stream()
                .map(CompletableFuture::join)
                .sorted(Comparator.comparing(PriceResultVo::getRealPrice))
                .findFirst()
                .get();
    }
}
